import axios from "axios";
import qs from "qs";
import { BASE_API_URL } from "@/conf/index";
const TIMEOUT = 10000;
const Service = axios.create({
	baseURL: BASE_API_URL,
	timeout: TIMEOUT,
});
Service.interceptors.request.use((config) => {
	if (config.method !== undefined && config.method.toLocaleUpperCase() === "POST") {
		config.data = qs.stringify(config.data);
		config.headers["Content-Type"] = "application/x-www-form-urlencoded";
	}
	return config;
});
// 添加响应拦截器
Service.interceptors.response.use(
	(response) => {
		const { status } = response.data;
		if (status === 200) {
			// todo;
		}
		return response.data;
	},
	(error) => {
		console.log(error);
		const { response } = error;
		if (response) {
			if (response.status === 404) {
				alert(`接口没找到哦～`);
			} else {
				alert(`服务器接口出错`);
			}
		} else {
			console.log(response);
		}
		return Promise.reject(error);
	}
);
export default Service;
